(function() {"use strict";var __module = CC_EDITOR ? module : {exports:{}};var __filename = 'preview-scripts/assets/resources/Script/Ssg/API/Native/NativeHttp.js';var __require = CC_EDITOR ? function (request) {return cc.require(request, require);} : function (request) {return cc.require(request, __filename);};function __define (exports, require, module) {"use strict";
cc._RF.push(module, 'b4e1bXTrUZM0LA7Or7IhaaZ', 'NativeHttp', __filename);
// resources/Script/Ssg/API/Native/NativeHttp.js

'use strict';

var NativeHttp = cc.Class({

    statics: {
        get: function get(responseType, url, stateChangeCallback) {
            var xhr = new XMLHttpRequest();
            xhr.responseType = responseType;
            var time = false; //是否超时
            var self = this;
            var timer = setTimeout(function () {
                //做请求超时操作
                if (timer == null) {
                    return;
                }
                time = true;
                xhr.abort(); //请求中止
                stateChangeCallback(true);
            }, 10000);
            xhr.onreadystatechange = function () {
                if (time) {
                    //请求已经超时，忽略中止请求
                    return;
                }
                if (xhr.readyState == 4) {
                    clearTimeout(timer); //取消等待的超时
                    timer = null;
                    UIHelper.hideLoaing();
                    if (xhr.status != 200) {
                        Log.error(url + ' - 请求回馈状态码为:', xhr.status);
                        var failMsg = HttpUtil.getTextWithStatus(xhr.status);
                        AddPrefabHelper.addMsgTipsUI(failMsg, true);
                        return;
                    }
                    stateChangeCallback(false, xhr);
                }
            };
            xhr.open('GET', encodeURI(url), true); //encodeURI(url)
            this.setHttpHeaderCookie(xhr);
            xhr.send();
            return xhr;
        },

        post: function post(responseType, url, params, stateChangeCallback, isMustCallback) {
            // cc.log('----------------in sendPostResponese_url: ' + url);
            var xhr = new XMLHttpRequest();
            xhr.responseType = responseType || 'arraybuffer';
            var time = false; //是否超时
            var self = this;
            var timer = setTimeout(function () {
                if (timer == null) {
                    return;
                }
                time = true;
                //做请求超时操作
                xhr.abort(); //请求中止
                self.doTimeOut(url);
                stateChangeCallback(true);
            }, 10000);
            xhr.onreadystatechange = function () {
                if (time) //请求已经超时，忽略中止请求
                    {
                        return;
                    }
                if (xhr.readyState == 4) {
                    clearTimeout(timer); //取消等待的超时
                    timer = null;
                    UIHelper.hideLoaing();
                    if (xhr.status != 200) {
                        Log.error(url + ' - 请求回馈状态码为:', xhr.status);
                        var failMsg = HttpUtil.getTextWithStatus(xhr.status);
                        AddPrefabHelper.addMsgTipsUI(failMsg, true);
                        return;
                    }
                    stateChangeCallback(false, xhr);
                }
            };
            xhr.open('POST', url, true);
            xhr.setRequestHeader("Content-Type", "application/x-protobuf");
            xhr.setRequestHeader("Accept", "application/x-protobuf");
            this.setHttpHeaderCookie(xhr);
            if (params) {
                xhr.send(params);
            } else {
                xhr.send();
            }
            return xhr;
        },

        setHttpHeaderCookie: function setHttpHeaderCookie(xhr) {
            xhr.setRequestHeader("nafosCookie", UserInfo.sessionId);
        }

    }
});

module.exports = NativeHttp;

cc._RF.pop();
        }
        if (CC_EDITOR) {
            __define(__module.exports, __require, __module);
        }
        else {
            cc.registerModuleFunc(__filename, function () {
                __define(__module.exports, __require, __module);
            });
        }
        })();
        //# sourceMappingURL=NativeHttp.js.map
        